//
//  UIDevice+ADUtil.m
//  ADKit
//
//  Created by duzexu on 2018/4/11.
//

#import "UIDevice+ADUtil.h"
#import "UIDevice+YYAdd.h"
#import "ADKitHeaders.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@implementation UIDevice (ADUtil)

+ (ADNetworkType)ad_networkReachType {
    switch ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus) {
        case AFNetworkReachabilityStatusUnknown:
            return ADNetworkUnknown;
            break;
        case AFNetworkReachabilityStatusNotReachable:
            return ADNetworkUnReachable;
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            return ADNetworkTypeWiFi;
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:{
            CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
            NSString *currentStatus = info.currentRadioAccessTechnology;
            if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
                return ADNetworkTypeGPRS;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
                return ADNetworkType2_75G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
                return ADNetworkType3G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
                return ADNetworkType3_5G_HSDPA;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
                return ADNetworkType3_5G_HSUPA;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
                return ADNetworkType2G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
                return ADNetworkType3G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
                return ADNetworkType3G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
                return ADNetworkType3G;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
                return ADNetworkTypeHRPD;
            }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
                return ADNetworkType4G;
            }else{
                return ADNetworkUnknown;
            }
        }
            break;
        default:
            return ADNetworkUnknown;
            break;
    }
}

+ (NSString *)ad_machineModelName {
    static dispatch_once_t one;
    static NSString *machineName;
    dispatch_once(&one, ^{
        NSString *model = UIDevice.currentDevice.machineModelName;
        if (!model) return;
        NSDictionary *dic = @{
                              @"iPhone10,1" : @"iPhone 8",
                              @"iPhone10,4" : @"iPhone 8",
                              @"iPhone10,2" : @"iPhone 8 Plus",
                              @"iPhone10,5" : @"iPhone 8 Plus",
                              @"iPhone10,3" : @"iPhone X",
                              @"iPhone10,6" : @"iPhone X",
                              @"iPhone11,8" : @"iPhone XR",
                              @"iPhone11,2" : @"iPhone XS",
                              @"iPhone11,4" : @"iPhone XS Max",
                              @"iPhone11,6" : @"iPhone XS Max",
                              
                              @"iPad6,11" : @"iPad 5",
                              @"iPad6,12" : @"iPad 5",
                              @"iPad7,1" : @"iPad Pro 2 (12.9 inch)",
                              @"iPad7,2" : @"iPad Pro 2 (12.9 inch)",
                              @"iPad7,3" : @"iPad Pro (10.5 inch)",
                              @"iPad7,4" : @"iPad Pro (10.5 inch)",
                              @"iPad7,5" : @"iPad 6",
                              @"iPad7,6" : @"iPad 6",
                              };
        machineName = dic[model];
        if (!machineName) machineName = model;
    });
    return machineName;
}

@end
